﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ADayRoiAdmin
{
    public partial class frmBusiness : Form
    {
        public frmBusiness()
        {
            InitializeComponent();
        }

        static SqlConnection con = null;
        private BindingManagerBase bm = null;
        private string temp_picture = string.Empty;
        AdayroiClass adayroi = new AdayroiClass(ref con);
        DataTable dt_business;

        private void frmBusiness_Load(object sender, EventArgs e)
        {
            DataTable dt_category = adayroi.getList("select * from Category where (PKID = 2 or PKID = 3) and IsDisplay = 'TRUE' order by Sort ", con);
            cb_Category.DataSource = dt_category;
            cb_Category.DisplayMember = "Name";
            cb_Category.ValueMember = "PKID";
            cb_Category.SelectedIndex = 0;
            dgv_BusinessLoad();
            ListHinhAnhLoad();
        }
     
        private void ListHinhAnhLoad()
        {
            try
            {               
                string sql = "select * from Business";
                dt_business = adayroi.getList(sql,con);        
                dgvBusiness.DataSource = dt_business.DefaultView;
                bm = dgvBusiness.BindingContext[dgvBusiness.DataSource, dgvBusiness.DataMember];
            }
            catch { }
        }

        private void dgv_BusinessLoad()
        {
            DataGridViewTextBoxColumn Business_ID = new DataGridViewTextBoxColumn();
            Business_ID.DataPropertyName = "Business_ID";
            Business_ID.Name = "Business_ID";
            Business_ID.Visible = false;

            DataGridViewTextBoxColumn Images = new DataGridViewTextBoxColumn();
            Images.DataPropertyName = "Images";
            Images.FillWeight = 30F;
            Images.Name = "Images";

            DataGridViewTextBoxColumn Name = new DataGridViewTextBoxColumn();
            Name.DataPropertyName = "Name";
            Name.HeaderText = "Name";
            Name.Name = "Name";

            DataGridViewTextBoxColumn Address = new DataGridViewTextBoxColumn();
            Address.DataPropertyName = "Address";
            Address.Name = "Address";

            DataGridViewTextBoxColumn Sort = new DataGridViewTextBoxColumn();
            Sort.DataPropertyName = "Sort";
            Sort.Name = "Sort";
            Sort.Visible = false;

            DataGridViewTextBoxColumn CellPhone = new DataGridViewTextBoxColumn();
            CellPhone.DataPropertyName = "CellPhone";
            CellPhone.Name = "CellPhone";
            CellPhone.HeaderText = "CellPhone";

            DataGridViewTextBoxColumn Email = new DataGridViewTextBoxColumn();
            Email.DataPropertyName = "Email";
            Email.Name = "Email";
            Email.HeaderText = "Email";

            DataGridViewTextBoxColumn Description = new DataGridViewTextBoxColumn();
            Description.DataPropertyName = "Description";
            Description.Name = "Description";
            Description.HeaderText = "Description";

             DataGridViewCheckBoxColumn IsDisplay = new DataGridViewCheckBoxColumn();
            IsDisplay.DataPropertyName = "IsDisplay";
            IsDisplay.Name = "IsDisplay";
            IsDisplay.HeaderText = "IsDisplay";


            dgvBusiness.Columns.AddRange(new DataGridViewColumn[] { Business_ID, Images, Name,Address,Sort,CellPhone,Email,Description,IsDisplay });
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string picture = adayroi.get_path_pic();
            temp_picture = adayroi.loadpicture(picture, pictureBox1);
        }

        private void dgvBusiness_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            if (findRow["Images"].ToString() != String.Empty)
                adayroi.lay_hinh
                    ("/Code/Upload/Business/" + findRow["Images"].ToString(), pictureBox1);
            txtName.Text = findRow["Name"].ToString();
            txtAddress.Text = findRow["Address"].ToString();
            txtcellphone.Text = findRow["CellPhone"].ToString();
            txtEmail.Text = findRow["Email"].ToString();
            txtDoUuTien.Text = findRow["Sort"].ToString();
            DataRow dr_category = adayroi.getList("select b.*, c.Parent from Business b left join Category c on b.CategoryID = c.PKID where b.Business_ID = " + findRow["Business_ID"].ToString(), con).Rows[0];
            cb_Category.SelectedValue = dr_category["Parent"].ToString();
            cb_CategorySub.SelectedValue = dr_category["CategoryID"].ToString();    
            chkhienthi.Checked = (bool)findRow["isDisplay"];
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (adayroi.ftp_picture(temp_picture, "/Code/Upload/Business/" + Path.GetFileName(temp_picture)) == 0)
            {
                string sql = "insert into Business (Images,Name,Address,CellPhone,Email,IsDisplay,Sort,CategoryID) values ('" + Path.GetFileName(temp_picture) + "','" + txtName.Text +
                    "','" + txtAddress.Text + "','" + txtcellphone.Text + "','" + txtEmail.Text + "','" + chkhienthi.Checked + "'," + txtDoUuTien.Text + "," + cb_CategorySub.SelectedValue.ToString() + ")";
                adayroi.UpdateSQL(con, sql);
                ListHinhAnhLoad();
            }
            else
            {
                MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            if (!String.IsNullOrEmpty(temp_picture) && Path.GetFileName(temp_picture) != findRow["Images"].ToString())
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Business/", findRow["Images"].ToString());

            if (adayroi.ftp_picture(temp_picture, "/Code/Upload/Business/" + Path.GetFileName(temp_picture)) == 0)
            {
                string str_pic = Path.GetFileName(temp_picture);
                if (str_pic == "") str_pic = findRow["Images"].ToString();
                string sql = "Update Business set Images = '" + str_pic
                    + "', Name = '" + txtName.Text + "', Address = '" + txtAddress.Text + "',CellPhone = '" + txtcellphone.Text
                    + "', Email = '" + txtEmail.Text + "', IsDisplay = '" + chkhienthi.Checked
                    + "', Sort = " + txtDoUuTien.Text + ", CategoryID = "+cb_CategorySub.SelectedValue.ToString()+ " where Business_ID = " + findRow["Business_ID"].ToString();
                adayroi.UpdateSQL(con, sql);
                ListHinhAnhLoad();
            }
            else
            {
                MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            }

        }

        private void btnXoa_Click_1(object sender, EventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            DialogResult response = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Xóa dòng?", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button2);
            if (response == DialogResult.Yes)
            {
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Business/", findRow["Images"].ToString());
                string sql = "delete from Business where Business_ID = " + findRow["Business_ID"].ToString();
                adayroi.UpdateSQL(con, sql);
                ListHinhAnhLoad();
            }
            else return;
        }

        private void dgvBusiness_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            DialogResult response = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Xóa dòng?", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button2);
            if (response == DialogResult.Yes)
            {
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Business/", findRow["Images"].ToString());
                string sql = "delete from Business where Business_ID = " + findRow["Business_ID"].ToString();
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                ListHinhAnhLoad();
            }
            else e.Cancel = true;
        }

        private void cb_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt_category2 = adayroi.getList("select * from Category where Parent = " + cb_Category.SelectedValue.ToString() + " and IsDisplay = 'TRUE' order by Sort ", con);
                cb_CategorySub.DataSource = dt_category2;
                cb_CategorySub.DisplayMember = "Name";
                cb_CategorySub.ValueMember = "PKID";
            }
            catch { }
        }

    }
}
